/*
 * 文 件 名:  BaseAction.java
 * 版    权:  xx Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  WQB
 * 修改时间:  2012-10-28
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.wqb.action;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;

/**
 * <一句话功能简述>
 * <功能详细描述>
 * 
 * @author  wqb
 * @version  [版本号, 2012-10-28]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */

public class BaseAction extends ActionSupport implements SessionAware {

    /**
     * 注释内容
     */
    private static final long serialVersionUID = -303779117382232923L;

    private static final String ACTION_MESSAGES = "actionMessages";

    private static final String ACTION_ERRORS = "actionErrors";

    protected final Logger logger = LoggerFactory.getLogger(getClass());
 
    protected Map session;

    /**
     * 跨redirect的增加ActionError.
     */
    public void addSessionActionError(String error) {
        super.addActionError(error);
        session.put(ACTION_ERRORS, super.getActionErrors());
    }

    /**
     * 跨redirect的增加ActionMessage.
     */
    @Override
    public void addActionMessage(String message) {
        super.addActionMessage(message);
        session.put(ACTION_MESSAGES, super.getActionMessages());
    }

    @Override
    public void clearErrorsAndMessages() {
        clearErrors();
        clearMessages();
        super.clearErrorsAndMessages();
    }

    @Override
    public Collection getActionErrors() {
        Collection errors = super.getActionErrors();

        if (errors.isEmpty()) {
            errors = (Collection) session.get(ACTION_ERRORS);
            if (!errors.isEmpty()) {
                super.setActionErrors(errors);
                clearErrors();
            }
        }
        return errors;
    }

    @Override
    public Collection getActionMessages() {
        Collection messages = super.getActionMessages();

        if (messages.isEmpty()) {
            messages = (Collection) session.get(ACTION_MESSAGES);
            if (!messages.isEmpty()) {
                super.setActionMessages(messages);
                clearMessages();
            }
        }
        return messages;
    }

    public void clearErrors() {
        session.remove(ACTION_ERRORS);
    }

    public void clearMessages() {
        session.remove(ACTION_MESSAGES);
    }

    /**
     * 绕过Template,直接输出内容的简便函数. 
     */
    protected String render(String text, String contentType) {
        try {
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setContentType(contentType);
            response.getWriter().write(text);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * 直接输出字符串.
     */
    protected String renderText(String text) {
        return render(text, "text/plain;charset=UTF-8");
    }

    /**
     * 直接输出HTML.
     */
    protected String renderHtml(String html) {
        return render(html, "text/html;charset=UTF-8");
    }

    /**
     * 直接输出XML.
     */
    protected String renderXML(String xml) {
        return render(xml, "text/xml;charset=UTF-8");
    }

    public void setSession(Map session){
        this.session = session;
    }
}

